import date_js from "@script/date";

export default {
    /**
     * 获取值班的 周几-时段-频率 列表
     */
    getDutyTimes() {
        let lists = this.buildDutyOfDay()
        let sub_menu = this.buildDutyOfRate(lists);
        return this.buildDutyOfWeek(sub_menu)
    },
    /**
     * 生成每天的各个时段
     */
    buildDutyOfDay() {
        return ['空', '上午一二', '上午三四', '下午一二', '下午三四'];
    },
    /**
     * 针对每天的各个时间，添加单双周子选项
     * @param {Array} lists 一天的各个时段
     */
    buildDutyOfRate(lists) {
        let child = []
        for (let i = 0; i < lists.length; i++) {
            child.push({
                value: i,
                label: lists[i],
                children: [
                    {
                        value: 'E',
                        label: '空'
                    },
                    {
                        value: 's',
                        label: '单',
                    },
                    {
                        value: 'd',
                        label: '双',
                    }
                ]
            })
        }
        return child
    },
    /**
     * 生成周一到周五，并将子菜单添加进去
     * @param {Array} sub_menu 子菜单（一天的各个时段）
     */
    buildDutyOfWeek(sub_menu) {
        let dutyList = []
        for (let i = 0; i <= 5; i++) {
            let week = {
                value: i,
                label: i == 0 ? "空" : date_js.weekOfToday(i),
                children: sub_menu
            };
            dutyList.push(week);
        }
        return dutyList
    },
    /**
     * 将每个学生的 duty 属性分解成级联框能识别的格式
     * @param {Array} stu_list 学生列表
     */
    parseDuty(stu_list, groups = -1) {
        for (let index = 0; index < stu_list.length; index++) {
            const item = stu_list[index]
            const duty = item.duty
            let duty_data = [0, 0, 'E']
            switch(duty.charAt(0)) {
                case 's': duty_data[2] = 's'; break
                case 'd': duty_data[2] = 'd'; break
            }
            let ch
            if (!isNaN(ch = duty.charAt(1))) {
                duty_data[0] = Number.parseInt(ch)
            }
            if (!isNaN(ch = duty.charAt(2))) {
                duty_data[1] = Number.parseInt(ch)
            }
            item.dutyList = duty_data
            if (groups != -1)
                item.gList = groups
        }
        return stu_list
    }
}